package edu.hwadee.BEIS.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 访问拦截器
 * @author kingsong
 *
 */
@Component
@ComponentScan(basePackages={"edu.hwadee.BEIS.aop"})
public class LoginInterceptor implements HandlerInterceptor {

	/**
	 * 检测登录状态
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		System.out.println("LoginInterceptor");
		boolean flag = true;
		String user_id = (String) request.getSession().getAttribute("user_id");
		String user_type = (String) request.getSession().getAttribute("user_type");
		if(null==user_id || null==user_type) {
			response.sendRedirect("/html/login.html");
			flag = false;
		}
		return flag;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
